var _grounpId;
var _ip;

function ReCreateKnapsack(ip, grounpId) {
	DeleteAllMarker();
	CreateRectangle();
	StartCreate(ip, grounpId);
}

function InitKnapsack(ip, grounpId) {
	_grounpId = grounpId;
	_ip = ip;
	var requestUrl = "http://" + ip + ":8897/GetKnapsackDetailList/" + grounpId;
	axios.get(requestUrl)
		.then(function(response) {
			//mui.alert('操作成功');
			if (response.data.length > 0) {
				if (response.data.length > 0) {
					for (var i = 0; i < response.data.length; i++) {
						var id = response.data[i].id;
						var infoId = response.data[i].knapsack_info_id;
						var _name = response.data[i].knapsack.name;
						var lat = response.data[i].lat;
						var lon = response.data[i].lon;
						var _type = response.data[i].knapsack.type;
						var addCount = response.data[i].addCountValue;

						createFenceMarker(lon, lat, _type, addCount, _name, id, infoId, map, true);
					}
				}

			} else {
				ReCreateKnapsack(ip, grounpId);
			}
		})
		.catch(function(error) {
			console.log(error);
			mui.alert('数据获取异常');
		});

}

var southWest;
var northEast;
var rectangle;

function CreateRectangle() {
	var bounds = circle.getBounds();
	southWest = bounds.getSouthWest();
	northEast = bounds.getNorthEast();

	// rectangle = new AMap.Rectangle({
	//         bounds: bounds,
	//         strokeColor:'red',
	//         strokeWeight: 1,
	//         strokeOpacity:0.5,
	//         strokeDasharray: [30,10],
	//         // strokeStyle还支持 solid
	//         strokeStyle: 'dashed',
	//         fillColor:'blue',
	//         fillOpacity:0.0,
	//         cursor:'pointer',
	//         zIndex:50,
	//     })

	//     rectangle.setMap(map)
}

function StartCreate(ip, grounpId) {
	var requestUrl = "http://" + ip + ":8897/GetKnapsackInfo/" + grounpId;
	axios.get(requestUrl)
		.then(function(response) {

			if (response.data.length > 0) {
				for (var i = 0; i < response.data.length; i++) {
					var count = response.data[i].count;
					var infoId = response.data[i].id;
					var _name = response.data[i].knapsack.name;
					var _type = response.data[i].knapsack.type;
					CreateRandomMarker(_type, count, _name, infoId);
				}
			} else {
				mui.alert('设备列表为空');
			}

		})
		.catch(function(error) {
			console.log(error);
			mui.alert('数据获取异常');
		});

}


function CreateRandomMarker(_type, _count, _name, infoId) {


	_minLng = southWest.getLng();
	_minLat = southWest.getLat();
	_maxLng = northEast.getLng();
	_maxLat = northEast.getLat();

	var count = 0;

	var Factor = 1000000;
	while (true) {

		var _lng = RandomNum(_minLng * Factor, _maxLng * Factor);
		var _lat = RandomNum(_minLat * Factor, _maxLat * Factor);
		var _position = [_lng / Factor, _lat / Factor];

		if (circle.contains(_position)) {


			count++;
			createFenceMarker(_lng / Factor, _lat / Factor, _type, 1, _name, 0, infoId, map, true);
			if (count > _count - 1) {
				break;

			}
		}
	}

}


var kmarkArray = new Array();

function createFenceMarker(lng, lat, _type, addCount, _name, id, inforId, map, isDraggable) {

	var markerContent =
		`<div style="pointer-events: none;">
			<img src="image/knapsack_${_type}.png" style="width:25px;height:25px"> 
			</div>`;
	var _markObject = new AMap.Marker({
		map: map,
		content: markerContent,
		title: _name,
		position: [lng, lat],
		draggable: isDraggable,
	});
	_markObject.id = id;
	_markObject.lng = lng;
	_markObject.lat = lat;
	_markObject.inforId = inforId;
	_markObject.iconType = _type;
	_markObject.addCountValue = addCount;
	kmarkArray.push(_markObject);


}

function CreatePlayerKnapsackMarker(lng, lat, _type, addCount, _name, id, inforId, map, _disntance) {

	_disntance = _disntance + "M ";
	var markerContent =
		`<div style="hCenter">
				<div  class="amap-marker-knapsack">${_disntance}</div>
				<img src="image/knapsack_${_type}.png" style="width:30px;height:30px;pointer-events: none;"> 
			   </div>`;

	var _markObject = new AMap.Marker({
		map: map,
		content: markerContent,
		title: _name,
		position: [lng, lat],
		draggable: false,
	});
	_markObject.id = id;
	_markObject.lng = lng;
	_markObject.lat = lat;
	_markObject.inforId = inforId;
	_markObject.iconType = _type;
	_markObject.addCountValue = addCount;
	kmarkArray.push(_markObject);
}

///获取当前背包的样式
function GetMarkerStyle(lng, lat, _type, addCountValue) {
	var markerContent =
		`<div style="pointer-events: none;">
							<img src="image/knapsack_${_type}.png" style="width:25px;height:25px"> 
						   </div>`;
	var _disntance = Math.round(AMap.GeometryUtil.distance(currrentMark.getPosition(), [lng, lat]));
	if (_disntance <= animationMinDistance) {
		_disntance = _disntance + "米";
		if (_type == 0) {
			_disntance += "，" + addCountValue + "命值";
		} else if (_type == 1) {
			_disntance += "，" + addCountValue + "弹量";
		} else if (_type == 2) {
			_disntance += "，无敌" + addCountValue + "分钟";
		} else if (_type == 3) {
			_disntance += "，金币" + addCountValue + "枚";
		}

		markerContent =
			`<div style="hCenter">
					<img src="image/knapsack_${_type}.png" style="width:30px;height:30px;pointer-events: none;"> 
					<div  class="amap-marker-knapsack">${_disntance}</div>
				   </div>`;
	}

	return markerContent;

}

//背包提醒的最短距离
const animationMinDistance = 99;

function SetMarkerDistanceShow() {
	if (kmarkArray != null && kmarkArray.length > 0 && currrentMark != null) {
		for (var i = 0; i < kmarkArray.length; i++) {
			kmarkArray[i].setAnimation("AMAP_ANIMATION_NONE");
			var tempPosition = kmarkArray[i].getPosition();
			var _disntance = Math.round(AMap.GeometryUtil.distance(tempPosition, currrentMark.getPosition()));
			
			kmarkArray[i].setContent(GetMarkerStyle(tempPosition.getLng(), tempPosition.getLat(), kmarkArray[i].iconType,
				kmarkArray[i].addCountValue));

		}
	}
}



function DeleteAllMarker() {
	for (var i = 0; i < kmarkArray.length; i++) {
		var markObject = kmarkArray[i];
		if (markObject) {
			markObject.setMap(null);
			markObject = null;
		}
	}
	kmarkArray = new Array();
}

function RandomNum(minNum, maxNum) {
	switch (arguments.length) {
		case 1:
			return parseInt(Math.random() * minNum + 1, 10);
			break;
		case 2:
			return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
			break;
		default:
			return 0;
			break;
	}
}
